<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\activity\controller;

use plugin\saiadmin\basic\BaseController;
use app\activity\logic\activityLogic;
use app\activity\validate\activityValidate;
use support\Request;
use support\Response;

/**
 * 活动列表控制器
 */
class activityController extends BaseController
{
    /**
     * 数据表主键
     */
    protected $pk = 'activity_id';
    
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new activityLogic();
        $this->validate = new activityValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['activity_category_id', ''],
            ['activity_start_time', ''],
            ['activity_end_time', ''],
            ['is_display', ''],
            ['activity_title', ''],
            ['activity_status','']
        ]);
        $query = $this->logic->search($where) ->order('sort_order desc,create_time desc');
        $data = $this->logic->getList($query);
        return $this->success($data);
    }
    public function updateSortOrder(Request $request) : Response
    {
        $id = $request->input('id', '');
        $sort_order = $request->input('sort_order', 1);
        
       
        $result = $this->logic->where($this->pk, $id)->update(['sort_order' => $sort_order,'update_time'=>date('Y-m-d H:i:s')]);
          
        if ($result) {
            // $this->afterChange('changeStatus');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
    public function changeStatus(Request $request) : Response
    {
        $id = $request->input('id', '');
        $status = $request->input('status', 1);
        $result = $this->logic->where($this->pk, $id)->update(['is_display' => $status]);
        if ($result) {
            $this->afterChange('changeStatus');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
    public function getActivityClass(Request $request): Response
    {
       
        $result = $this->logic->whereNull('delete_time')
        ->field('activity_id as value,activity_title as label')
        ->order('sort_order desc')
        ->order('update_time desc')
        ->select()
        ->toArray();
        return $this->success($result,'获取成功');
    }
    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    public function save(Request $request) : Response
    {
        $data = $request->post();
        if ($this->validate) {
            if (!$this->validate->scene('save')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
        if($data['activity_start_time']<=date('Y-m-d H:i:s')
            && $data['activity_end_time']>=date('Y-m-d H:i:s')){
            $data['status']=0;
        }
        if($data['activity_end_time']<=date('Y-m-d H:i:s')){
            $data['status']=1;
        }
        $result = $this->logic->save($data);
        if ($result) {
            $this->afterChange('save');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 修改数据
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update(Request $request, $id) : Response
    {
        $id = $request->input('id', $id);
        $data = $request->post();
        if ($this->validate) {
            if (!$this->validate->scene('update')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
       
        $info = $this->logic->find($id);
        if (!$info) {
            return $this->fail('没有找到该数据');
        }
          if($data['activity_start_time']<=date('Y-m-d H:i:s')
            && $data['activity_end_time']>=date('Y-m-d H:i:s')){
            $data['status']=0;
        }
        if($data['activity_end_time']<=date('Y-m-d H:i:s')){
            $data['status']=1;
        }
        $result = $this->logic->update($data, [$this->pk => $id]);
        if ($result) {
            $this->afterChange('update');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
     public function export(Request $request) : Response
    {
       
         $where = $request->more([
            ['activity_category_id', ''],
            ['activity_start_time', ''],
            ['activity_end_time', ''],
            ['is_display', ''],
            ['activity_title', ''],
            ['activity_status','']
        ]);
        return $this->logic->export($where);
    }
}
